package org.makcro.sms.services.chat.web.client.mvc.view;

import org.makcro.sms.services.chat.web.client.AppEvents;
import org.makcro.sms.services.chat.web.client.Constants;
import org.makcro.sms.services.chat.web.client.services.subscriber.SubscriberServiceAsync;
import org.makcro.sms.services.chat.web.client.widgets.bulk.BulkMessagesSearchTabItem;
import org.makcro.sms.services.chat.web.client.widgets.main.MainTabPanel;
import org.makcro.sms.services.chat.web.client.widgets.subscirbers.SubscribersPanel;
import org.makcro.sms.services.chat.web.shared.dto.SubscriberDto;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.data.BasePagingLoader;
import com.extjs.gxt.ui.client.data.PagingLoadConfig;
import com.extjs.gxt.ui.client.data.PagingLoadResult;
import com.extjs.gxt.ui.client.data.PagingLoader;
import com.extjs.gxt.ui.client.data.RpcProxy;
import com.extjs.gxt.ui.client.event.EventType;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class SubscriberTabView extends View {

	private SubscribersPanel	subscribersPanel;
	private TabItem				subscribersTabItem;

	public SubscriberTabView(Controller controller) {
		super( controller );
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void handleEvent(AppEvent event) {
		EventType type = event.getType();

		if (type.equals( AppEvents.SUBSCRIBERS_TAB_REQUESTED )) {
			onSubscribersTabRequested( event );
		}

	}

	private void onSubscribersTabRequested(AppEvent event) {
		MainTabPanel mainTabPanel = Registry.get( Constants.Ui.MAIN_TAB_PANEL );
		TabItem tabItem = getSubscribersTabItem();
		mainTabPanel.add( tabItem );
		mainTabPanel.setSelection( tabItem );

	}

	public SubscribersPanel getSubscribersPanel() {
		if (this.subscribersPanel == null) {
			// get bulk service
			final SubscriberServiceAsync subscriberService = Registry.<SubscriberServiceAsync> get( Constants.Services.SUBSCRIBER_SERVICE );
			// create rpc proxy
			RpcProxy<PagingLoadResult<SubscriberDto>> rpcProxy = new RpcProxy<PagingLoadResult<SubscriberDto>>() {

				@Override
				protected void load(Object loadConfig, AsyncCallback<PagingLoadResult<SubscriberDto>> callback) {
					subscriberService.search( (PagingLoadConfig) loadConfig, callback );
				}
			};

			// init loader
			PagingLoader<PagingLoadResult<SubscriberDto>> pagingLoader = new BasePagingLoader<PagingLoadResult<SubscriberDto>>( rpcProxy );
			this.subscribersPanel = new SubscribersPanel( pagingLoader );
			this.subscribersPanel.setHeading( "Pretraga korisnika" );
		}
		return subscribersPanel;
	}

	public TabItem getSubscribersTabItem() {
		if (this.subscribersTabItem == null) {
			this.subscribersTabItem = new TabItem( "Korisnici" );
			this.subscribersTabItem.setClosable( true );
			this.subscribersTabItem.setLayout( new FitLayout() );
			this.subscribersTabItem.add( getSubscribersPanel() );
		}
		return subscribersTabItem;
	}
}
